package com.cskaoyan.controller.ControllerByshj;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.RolePermissionVo;
import com.cskaoyan.mapper.PeriMissionListMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022/05/05 17:19
 * 管理原管理
 *
 * @author 孙豪骏
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    LogService logService;
    @Autowired
    PeriMissionListMapper periMissionListMapper;
    @Autowired
    RoleMapper roleMapper;

    //负责显示管理员列表
    @ResponseBody
    @RequestMapping("/admin/list")
    public BaseRespVo adminList(BaseParam baseParam, String username) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        if (username != null) {
            username = "%" + username + "%";
        }
        List<AdminListVoBean> admins = adminService.queryAdminListInList(baseParam.getOrder(), baseParam.getSort(), username);
        PageInfo<AdminListVoBean> adminListVoBeanPageInfo = new PageInfo<>(admins);
        AdminData adminData = new AdminData();
        adminData.setTotal((int) adminListVoBeanPageInfo.getTotal());
        adminData.setPages(adminListVoBeanPageInfo.getPages());
        adminData.setPage(adminListVoBeanPageInfo.getPageNum());
        adminData.setLimit(baseParam.getLimit());
        adminData.setList(admins);
        return BaseRespVo.okSuccess(adminData, "成功");
    }


    //负责管理原的创建
    @ResponseBody
    @RequestMapping("/admin/create")
    public BaseRespVo createAdmin(@RequestBody AdminListVoBean admin) {


        if ("".equals(admin.getUsername()) || admin.getUsername().length() < 3) {
            return BaseRespVo.getResult(null, "管理员名字太短", 601);
        }
        if ("".equals(admin.getPassword()) || admin.getPassword().length() < 6) {
            return BaseRespVo.getResult(null, "密码不能小于6", 602);
        }
        if (admin.getRoleIds() == null || admin.getRoleIds().length == 0) {
            return BaseRespVo.getResult(null, "你还未选取管理员角色", 601);
        }
        //成功后插入market_admin表
        //插入之前先查询
        if (adminService.queryAdmin(admin) != null) {
            return BaseRespVo.getResult(null, "你已经创建过此名字管理员", 601);
        }
        adminService.insertAdmin(admin);
        //因为返回值需要，查询一下，封装,根据用户名查询
        AdminListVoBean adminListVoBean = adminService.queryAdmin(admin);
        return BaseRespVo.getResult(adminListVoBean, "成功", 0);

    }


    //负责管理原的编辑
    @ResponseBody
    @RequestMapping("/admin/update")
    public BaseRespVo updateAdmin(@RequestBody AdminListVoBean admin) {

        //根据得到的东西跟新
        adminService.updateAdmin(admin);
        AdminListVoBean adminListVoBean = adminService.queryAdmin(admin);
        return BaseRespVo.getResult(adminListVoBean, "成功", 0);
    }

    //负责删除管理员列表
    @ResponseBody
    @RequestMapping("/admin/delete")
    public BaseRespVo deleteAdmin(@RequestBody AdminListVoBean admin) {

        //根据得到的东西跟新
        adminService.deleteAdmin(admin);
        return BaseRespVo.getResult(null, "成功", 0);
    }

    //负责显示管理员列表里的管理原角色
    @ResponseBody
    @RequestMapping("/role/options")
    public BaseRespVo adminOption() {

        TotalPageLimit totalPageLimit = new TotalPageLimit();
        totalPageLimit.setTotal(5);
        totalPageLimit.setPage(1);
        totalPageLimit.setPages(1);
        totalPageLimit.setLimit(20);
        List<MarketRole> marketRoles = adminService.queryMarketRoleList();
        totalPageLimit.setList(marketRoles);
        return BaseRespVo.okSuccess(totalPageLimit, "成功");
    }


    @ResponseBody
    @RequestMapping("/role/list")
    public BaseRespVo adminRoleList(BaseParam baseParam, String name) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<Role> roles = adminService.queryRoleList(baseParam, name);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        AdminData adminData = new AdminData();
        adminData.setTotal((int) rolePageInfo.getTotal());
        adminData.setPages(rolePageInfo.getPages());
        adminData.setPage(rolePageInfo.getPageNum());
        adminData.setLimit(baseParam.getLimit());
        adminData.setList(roles);
        return BaseRespVo.okSuccess(adminData, "成功");
    }


    @ResponseBody
    @RequestMapping("/role/update")
    public BaseRespVo updateRoleList(@RequestBody Role role) {
        adminService.updateRole(role);
        return BaseRespVo.getResult(null, "成功", 0);
    }

    //TODO 还没测试
    @ResponseBody
    @RequestMapping("/role/delete")
    public BaseRespVo deleteRole(@RequestBody Role role) {

        adminService.deleteRole(role);
        return BaseRespVo.getResult(null, "成功", 0);
    }


    @ResponseBody
    @RequestMapping("/role/create")
    public BaseRespVo createRole(@RequestBody Role role) {
        adminService.insertRole(role);
        return BaseRespVo.getResult(null, "成功", 0);
    }


    @ResponseBody
    @GetMapping("/role/permissions")
    public BaseRespVo shouPermissionList(Integer roleId) {
        List<String> permissions = roleMapper.selectLinkPermissionByRoleId(roleId);
        RolePermissionVo rolePermissionVo = new RolePermissionVo();
        rolePermissionVo.setAssignedPermissions(permissions);
        ArrayList<SystemPermissionsDTO> systemPermissionsDTOS = new ArrayList<>();
        SystemPermissionsDTO one = new SystemPermissionsDTO("1", "商品管理", null);
        systemPermissionsDTOS.add(one);
        SystemPermissionsDTO two = new SystemPermissionsDTO("2", "推广管理", null);
        systemPermissionsDTOS.add(two);
        SystemPermissionsDTO three = new SystemPermissionsDTO("3", "超级管理员", null);
        systemPermissionsDTOS.add(three);
        rolePermissionVo.setSystemPermissions(systemPermissionsDTOS);
        return BaseRespVo.getResult(rolePermissionVo, "成功", 0);
    }

    @ResponseBody
    @PostMapping("/role/permissions")
    // @RequiresPermissions("admin:role/permissions")
    public BaseRespVo shouPermissionList2(Integer roleId) {
        List<String> permissions = roleMapper.selectLinkPermissionByRoleId(roleId);
        RolePermissionVo rolePermissionVo = new RolePermissionVo();
        rolePermissionVo.setAssignedPermissions(permissions);
        return BaseRespVo.getResult(rolePermissionVo, "成功", 0);
    }


    @ResponseBody
    @RequestMapping("/log/list")
    public BaseRespVo logList(BaseParam baseParam, String name) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<Log> logs = logService.queryLogList(baseParam, name);
        PageInfo<Log> rolePageInfo = new PageInfo<>(logs);
        //因为这里里面是范型，所以下面可以用
        AdminData adminData = new AdminData();
        adminData.setTotal((int) rolePageInfo.getTotal());
        adminData.setPages(rolePageInfo.getPages());
        adminData.setPage(rolePageInfo.getPageNum());
        adminData.setLimit(baseParam.getLimit());
        adminData.setList(logs);
        return BaseRespVo.okSuccess(adminData, "成功");
    }


}
